/**
 * 描述ajax的封装
 * @date 2021-05-02
 * @param {any} options
 * @returns {any}
 */
function request(options) {
    return new Promise((resolve, reject) => {
        let xhr = XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP')
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.readyState === 200) {
                let response = xhr.response
                // console.log(response)
                //如何查询返回的结果
                response = response ? JSON.parse(response) : {}
                resolve(response)
            }
        }
        let query = getPath(options.data)
        console.log(query)
        if (options.type === 'GET') {
            options.url += '?' + query
            xhr.open(options.type, options.url, true)
            xhr.setRequestHeader('token', getToken())
            xhr.send()
        } else if (options.type === 'POST') {
            xhr.open(options.type, options.url, true)
            xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
            xhr.setRequestHeader('token', getToken())
            xhr.send(query)
        }
    })
}
